import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('../views/login/Login')
  },
  {
    path: '/home',
    component: () => import('../views/home/Home'),
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: () => import('../views/home/Welcome') },
      { path: '/users', component: () => import('../views/user/Users') }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()

  const tokenStr = window.sessionStorage.getItem('token')

  if (!tokenStr) return next('/login')

  next()
})

export default router
